package common;

import java.io.File;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

public class ChanageDocToPdf {
	//0:doc \t 2:txt \t 8:html \t 9:htm \t 11:xml \t 12:docx \t 17:pdf \t 18:xps
    public static String chanage(String sourceFilePath,String destinationFilePath,int destinationFileType){
    	System.out.println("sourceFilePath-----------"+sourceFilePath);
    	System.out.println("destinationFilePath----------"+destinationFilePath);
    	File sourceFile=new File(sourceFilePath);
    	String fileName=sourceFile.getName();
    	String fileType = new String("");
		// 取得文件扩展名
		fileType = fileName.substring((fileName.length() - 4),fileName.length());
    	if(fileType.equals("docx")){
    		// 启动word
    		//ComThread.InitSTA();
			ActiveXComponent app=null;
			try {
				app = new ActiveXComponent("Word.Application");
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			destinationFilePath+="\\"+fileName.substring(0, (fileName.length() - 5));
			boolean flag = false;
			try {
				//设置word可见性
				app.setProperty("Visible", new Variant(false));
				Dispatch docs = app.getProperty("Documents").toDispatch();
				// 打开word文档
				Dispatch doc = Dispatch.invoke(docs,"Open",Dispatch.Method,new Object[] { sourceFilePath, new Variant(false),new Variant(true) }, new int[1]).toDispatch();
				System.out.println("-------"+destinationFilePath);
				Dispatch.invoke(doc, "SaveAs", Dispatch.Method,new Object[] { destinationFilePath,new Variant(destinationFileType) },new int[1]);
				Variant file = new Variant(false);
				Dispatch.call(doc, "Close", file);
				flag = true;
			} catch (Exception e) {
				e.printStackTrace();
				System.out.println("文档转换失败");
			} finally {
				app.invoke("Quit", new Variant[] {});
			}
			System.out.println("转换完毕");
    	}else if(fileType.equals(".doc")){
    		// 启动word
			ActiveXComponent app = new ActiveXComponent("Word.Application");
			destinationFilePath+="\\"+fileName.substring(0, (fileName.length() - 4));
			boolean flag = false;
			try {
				// 设置word可见性
				app.setProperty("Visible", new Variant(false));
				Dispatch docs = app.getProperty("Documents").toDispatch();
				// 打开word文档
				Dispatch doc = Dispatch.invoke(docs,"Open",Dispatch.Method,new Object[] { sourceFilePath, new Variant(false),new Variant(true) }, new int[1]).toDispatch();
				// 另存为指定格式的文档
				Dispatch.invoke(doc, "SaveAs", Dispatch.Method,new Object[] { destinationFilePath,new Variant(destinationFileType)},new int[1]);
				Variant file = new Variant(false);
				// 关闭文档
				Dispatch.call(doc, "Close", file);
				flag = true;
			} catch (Exception e){
				e.printStackTrace();
				System.out.println("文档转换失败");
			} finally {
				app.invoke("Quit", new Variant[] {});
			}
			System.out.println("转换完毕");
    	}
    	return destinationFilePath+".pdf";
    }
    public static void main(String[] args) {
		String result=chanage("E:\\Program Files\\apache-tomcat-7.0.30-windows-x64\\apache-tomcat-7.0.30\\webapps\\wrem\\uploads\\20150608\\4160ba36-9c46-4cda-8231-cce4a88913cb.doc","E:\\Program Files\\apache-tomcat-7.0.30-windows-x64\\apache-tomcat-7.0.30\\webapps\\wrem\\uploads\\20150608\\4160ba36-9c46-4cda-8231-cce4a88913cb",17);
		System.out.println(result);
	}
}
